import java.util.Arrays;
import java.util.Random;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 周斌
 * Date: 2024-03-31
 * Time: 10:23
 */
public class TimeTest {
    //有序数组(升序)
    public static void orderArray(int[] array) {
        for (int i = 0; i < array.length; i++) {
            array[i] = i;
        }
    }
    //有序的数组(降序)
    public static void notOrderArray(int[] array) {
        for (int i = array.length-1; i >=0 ; i--) {
            array[i] = i;
        }
    }
    //随机数组
    public static void randomArray(int[] array) {
        Random random = new Random();
        int count = 0;
        for (int i = 0; i < array.length; i++) {
            array[i] = random.nextInt(100000);
        }
    }

    //测试排序的运行速度

    public static void testInsertSort(int[] array) {
        //拷贝一份数组，防止排序会影响测试
        int[] tmpArray = Arrays.copyOf(array,array.length);
        long startTime = System.currentTimeMillis();
        Sort.insertSort(tmpArray);
        long endTime = System.currentTimeMillis();
        System.out.println("直接插入排序："+ (endTime - startTime));
    }
    public static void testShellSort(int[] array) {
        //拷贝一份数组，防止排序会影响测试
        int[] tmpArray = Arrays.copyOf(array,array.length);
        long startTime = System.currentTimeMillis();
        Sort.shellSort(tmpArray);
        long endTime = System.currentTimeMillis();
        System.out.println("希尔排序："+ (endTime - startTime));
    }
    public static void testSelectSort(int[] array) {
        //拷贝一份数组，防止排序会影响测试
        int[] tmpArray = Arrays.copyOf(array,array.length);
        long startTime = System.currentTimeMillis();
        Sort.selectSort1(tmpArray);
        long endTime = System.currentTimeMillis();
        System.out.println("选择排序："+ (endTime - startTime));
    }
    public static void testHeapSort(int[] array) {
        //拷贝一份数组，防止排序会影响测试
        int[] tmpArray = Arrays.copyOf(array,array.length);
        long startTime = System.currentTimeMillis();
        Sort.heapSort(tmpArray);
        long endTime = System.currentTimeMillis();
        System.out.println("堆排序："+ (endTime - startTime));
    }
    public static void testQuickSort(int[] array) {
        //拷贝一份数组，防止排序会影响测试
        int[] tmpArray = Arrays.copyOf(array,array.length);
        long startTime = System.currentTimeMillis();
        Sort.quickSort(tmpArray);
        long endTime = System.currentTimeMillis();
        System.out.println("快速排序："+ (endTime - startTime));
    }
    public static void testMergeSort(int[] array) {
        //拷贝一份数组，防止排序会影响测试
        int[] tmpArray = Arrays.copyOf(array,array.length);
        long startTime = System.currentTimeMillis();
        Sort.mergeSort(tmpArray);
        long endTime = System.currentTimeMillis();
        System.out.println("归并排序："+ (endTime - startTime));
    }

    public static void main(String[] args) {
        int[] array = new int[1_0000];
        //orderArray(array);
        //notOrderArray(array);
        randomArray(array);
        TimeTest.testInsertSort(array);
        TimeTest.testShellSort(array);
        //TimeTest.testSelectSort(array);
        TimeTest.testHeapSort(array);
        TimeTest.testQuickSort(array);
        TimeTest.testMergeSort(array);


    }
}
